/**
 * 打开新增周计划弹窗
 */
function add(){
    $.confirm({
        title: '新增周计划',
        content: 'url:' + ctx + "plan/addDialog" ,
        columnClass: 'col-md-8 col-md-offset-2 ',
        buttons: {
            取消: function () {
            },
            confirm: {
                text: '确定',
                btnClass: 'btn-blue',
                action: function () {
                    let formJson = this.$content.find("form").formJson();
                    if (!common.validateForm(formJson)) {
                        $.alert('请完整选择用户信息和周信息');
                        return false;
                    }
                    this.$content.find("form").submit();
                }
            }
        },
        onContentReady: function () {
            common.initWeekDatePicker('weekPicker1', 'weekRangePicker1', 'startWeekDate1', 'endWeekDate1', '.jconfirm');
        },
    });
}

/**
 * 新增一行
 * @type {*|jQuery}
 */
var $templateTr = $("#templateTr").clone().removeAttr("id");
function addRowDetail() {
    debugger;
    let $tr = $templateTr.clone();
    window.$nojs($tr);
    $("#tbody").append($tr);

}

/**
 * 删除一行记录
 */
function delRowDetail(event) {
    $.confirm({
        title: '删除计划',
        content: '确定删除本条计划吗？',
        columnClass: 'col-md-6 col-md-offset-3 ',
        buttons: {
            cancel: function () {
            },
            confirm: {
                text: '确定',
                btnClass: 'btn-blue',
                action: function () {
                    getRow(event).remove();
                }
            }
        }
    })
}

function getRow(event) {
    return $(event).parents("tr");
}

function submitBatch(event, submit) {
    let $trs = $("#tbody tr");
    let datas = [];
    let useId = $("#userId").val()
    let planId = $("#planId").val()
    let url = ctx + "plan/batchInsert?planId=" +  planId + "&submit=" + submit;
    for (let i = 0; i < $trs.length; i++) {
        let $tr = $trs.eq(i);
        let data = {
            userId:useId,
            planId:planId,
            id: $(":input[name='id']", $tr).val(),
            sequenceNumber: $(":input[name='sequenceNumber']", $tr).val(),
            description: $(":input[name='description']", $tr).val(),
            estimatedEffort: $("input[name='estimatedEffort']", $tr).val(),
            actualEffort: $(":input[name='actualEffort']", $tr).val(),
            plannedCompletionDate: $(":input[name='plannedCompletionDate']", $tr).val(),
            actualCompletionDate: $(":input[name='actualCompletionDate']", $tr).val(),
            needsSupport: $(":input[name='needsSupport']", $tr).val(),
            progress: $(":input[name='progress']", $tr).val(),
            status: $(":input[name='status']", $tr).val(),
        }
        if (!common.validateForm(data, 'actualCompletionDate')) {
            $.alert("请完整填写计划内容");
            return;
        }
        datas.push(data);
    }
    if (submit) {
        $.confirm({
            title: '确认提交工作计划吗!',
            content: '提交工作计划后将不可编辑，除非撤销！',
            columnClass:'col-md-12 ',
            buttons: {
                取消: function () {
                },
                确定: function () {
                    realSave();
                },
            }
        });
        return false;
    } else {
        realSave();
        return false;
    }


    function realSave() {
        $.ajax(url, {
            dataType: "json",
            type: "post",
            data: JSON.stringify(datas),
            contentType: "application/json",
            success: function (data) {
                if (data.code == 0) {
                    $.alert({
                        title: '成功',
                        content: '操作成功'

                    });
                    $("#backBtn")[0].click();
                } else {
                    $.alert({
                        title: '失败',
                        content: data.msg
                    });
                }
            }

        })
    };



}